package com.example.joewang.surfaceviewmotion.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.joewang.surfaceviewmotion.model.Snow;
import com.example.joewang.surfaceviewmotion.model.SnowManager;

import java.util.List;

/**
 * Created by simplecc on 2017/10/21.
 */

public class SnowSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private SnowThread mThread = null;

    public SnowSurfaceView(Context context) {
        this(context, null);
    }

    public SnowSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    private void init(Context context) {
        SurfaceHolder holder = this.getHolder();
        holder.addCallback(this);
        //添加背景透明
        this.setZOrderOnTop(true);
        holder.setFormat(PixelFormat.TRANSLUCENT);
        mThread = new SnowThread(context, holder, true);
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mThread.setIsRuan(true);
        mThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mThread.setIsRuan(false);
    }


    class SnowThread extends Thread {
        private Context context;
        private SurfaceHolder holder;
        private boolean isRun;
        private SnowManager snowManager;

        public SnowThread(Context context, SurfaceHolder holder, boolean isRun) {
            this.holder = holder;
            this.isRun = isRun;
            this.context = context;
        }

        public void setIsRuan(boolean flag) {
            this.isRun = flag;
        }

        @Override
        public void run() {
            Canvas c = null;
            snowManager = SnowManager.getInstance(context);
            List<Snow> all = snowManager.getAllSnows();
            final Paint p = new Paint();
            while (isRun) {
                c = holder.lockCanvas();
                if(c == null){
                    break;
                }
                p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                c.drawPaint(p);
                p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
                try {
                    synchronized (holder) {
                        for (Snow snow : all) {
                            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), snow.resId);
                            c.drawBitmap(bitmap, snow.getmXposition(), snow.getmYPosition(), p);
                            snow.updatePosition();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (c != null) {
                        holder.unlockCanvasAndPost(c);
                    }
                }
            }

        }
    }
}
